/*
 * Copyright 2008-2012 WebEqua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.webequa.websolve.services.generic;

import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONObject;
import org.mathcloud.everest.Job;
import org.mathcloud.everest.adapter.java.JavaServiceI;
import org.webequa.core.common.quantity.QuantityObject;
import org.webequa.core.common.quantity.RealMatrix;
import org.webequa.core.maple.quantity.MapleRealNumber;
import org.webequa.core.scilab.operator.algebra.ScilabInverse;
import org.webequa.io.scilab.ScilabQuantityObjectBuilder;
import org.webequa.io.string.generic.GenericOutputter;



public class SimpleService0 implements JavaServiceI 
{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(Map<String, String> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run(Job arg0) throws Exception {
		ScilabQuantityObjectBuilder builder = new ScilabQuantityObjectBuilder();
		GenericOutputter output = new GenericOutputter();			
		
//		MapleRealNumber m = new MapleRealNumber(5);
		
		QuantityObject res = builder.build(arg0.getInput(),"inMatrix");

		ScilabInverse inverse = new ScilabInverse();
		inverse.setInputParams(res);
		inverse.calculate();
		
		JSONObject jout = new JSONObject();
		jout.put("outMatrix", output.buildJSONArray((RealMatrix)inverse.getResult()));
		
		arg0.setOutput(jout);
//		arg0.setOutput(output.output("outMatrix", res));		
	}

	public static final Logger log = Logger.getLogger(SimpleService0.class);
}
